package cn.neusoft.controller;

import cn.neusoft.pojo.Address;
import cn.neusoft.query.AddressQuery;
import cn.neusoft.service.IAddressService;
import cn.neusoft.vo.ResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/address")
public class AddressController {
	@Autowired
	private IAddressService addressService;
	@PostMapping      //  请求方式 + 请求路径   post  /address
	public ResultBean save(@RequestBody Address address) throws Exception {
		System.out.println("----AddressController-----add--------");
		System.out.println(address);
		if(address.getAddressId()==null) {
			return addressService.add(address);
		}
		return  addressService.update(address);
	}
//	@PutMapping
//	public ResultBean upddate(@RequestBody Address address) throws Exception {
//		System.out.println("----AddressController-----update--------");
//		return addressService.update(address);
//	}
	@DeleteMapping("/{id}")
	public ResultBean delete(@PathVariable("id") Integer id) throws Exception {
		System.out.println("----AddressController-----delete--------");
		return addressService.delete(id);
	}
	@GetMapping("/{id}")
	public ResultBean getById (@PathVariable("id") Integer id) throws Exception {
		System.out.println("----AddressController-----getById--------");
		return addressService.getById(id);
	}
	@GetMapping("/user/{userId}")
	public ResultBean getByUserId(@PathVariable("userId") Integer id) throws Exception {
		System.out.println("----AddressController-----getById--------");
		return addressService.getByUserId(id);
	}
	@GetMapping("/all")
	public ResultBean getAll ( ) throws Exception {
		System.out.println("----AddressController-----getAll--------");
		return addressService.getAll();
	}
	@PostMapping("/page")
	public ResultBean getByPage(@RequestBody AddressQuery addressQuery) throws  Exception {
		System.out.println("----AddressController-----getByPage--------");
		System.out.println(addressQuery);
		return  addressService.getByPage(addressQuery);
	}

}
